import Vue from 'vue'
import VueRouter from 'vue-router'

//引入的登录组件
const Login  = () => import(/* webpackChunkName: "login-welcome-home" */ '../components/Login.vue')
 //欢迎页组件
const Welcome = () => import(/* webpackChunkName: "login-welcome-home" */ '../components/Welcome.vue')
//引入的Home页组件
const Home = () => import(/* webpackChunkName: "login-welcome-home" */ '../views/Home.vue')



//用户列表组件
const UserList = () => import(/* webpackChunkName: "userlist-rightslist-roleslist" */ '../views/users/UserList.vue')
 //权限列表组件
const RightsList= () => import(/* webpackChunkName: "userlist-rightslist-roleslist" */ '../views/rights/RightsList.vue')
 //用户管理组件
const RolesList= () => import(/* webpackChunkName: "userlist-rightslist-roleslist" */ '../views/rights/RolesList.vue')


 //商品分类组件
const CateClass= () => import(/* webpackChunkName: "cateClass-params" */ '../views/goods/CateClass.vue' )
 //商品分类参数组件
const Params= () => import(/* webpackChunkName: "cateClass-params" */ '../views/goods/Params.vue' )

//商品列表组件
const Goods= () => import(/* webpackChunkName: "goods-addgoods" */ '../views/goods/Goods.vue' )
//添加商品列表组件
const AddGoods= () => import(/* webpackChunkName: "goods-addgoods" */ '../views/goods/AddGoods.vue' )

//添加订单列表组件
const Orders= () => import(/* webpackChunkName: "Order-reports " */ '../views/order/Orders.vue' )
//添加数据统计组件
const Reports= () => import(/* webpackChunkName: "Order-reports " */ '../views/reports/Reports.vue' )

Vue.use(VueRouter)

const routes = [
 //主页组件
  {
    path: '/',
    redirect: '/login',
    component: Home
  },
  //登录组件
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  //Home页组件
  {
    path:'/home',
    name:'Home',
    redirect:'/welcome',
    children:[
      //欢迎页面组件
      {
        path:'/welcome',
        name:'Welcome',
        component:Welcome
      },
      //用户列表组件
      {
        path:'/users',
        name:'UserList',
        component:UserList
      },
      //权限列表组件
      {
        path:'/rights',
        name:'RightsList',
        component: RightsList
      },
      //角色列表组件
      {
        path:'/roles',
        name:'RolesList',
        component: RolesList
      },
      //权限列表组件
      {
        path:'/categories',
        name:'Categories',
        component: CateClass
      },
      //商品参数组件
      {
        path:'/Params',
        name:'Params',
        component: Params
      },
      //商品列表组件
      {
        path:'/goods',
        name:'Goods',
        component: Goods
      },
      //添加商品列表组件
      {
        path:'/AddGoods',
        name:'AddGoods',
        component: AddGoods
      },
      //添加订单列表组件
      {
        path:'/orders',
        name:'Orders',
        component: Orders
      },
         //数据统计组件
      {
        path:'/reports',
        name:'Reports',
        component: Reports
      }
    ],
    component:Home
  }

]

const router = new VueRouter({
  routes
})

//设置路由登录的导航守卫有权限直接访问要访问的页面，没有权限显示登录页面
router.beforeEach((to,form,next)=>{
     if(to.path == '/login') return next();

      let token = window.sessionStorage.getItem('token')
    if(!token) return next('/login')

      return next();
  
})

export default router
